package com.zwt.controller;

import com.alibaba.fastjson.JSON;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zwt.constant.UrlConstant;
import com.zwt.domain.TsPlatformUser;
import com.zwt.feign.UserFeign;
import com.zwt.service.ApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static javafx.scene.input.KeyCode.H;

/**
 * Created by zwt on 2018/8/24.
 */
@Controller
@RequestMapping("consumerMovie")
public class UserController {

    @Autowired
    private ApiService apiService;

    @Autowired
    private UrlConstant urlConstant;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private UserFeign userFeign;

    /**
     * 远程调用user服务 没有使用Eureka
     * @param id
     * @return
     */
    @RequestMapping("getUserStr")
    @ResponseBody
    public String getUserStr(Long id){
        String url = urlConstant.getUserUrl().replace("ID", String.valueOf(id));
        String json = apiService.getJson(url);
        TsPlatformUser tsPlatformUser = JSON.parseObject(json, TsPlatformUser.class);
        return tsPlatformUser.getNick()+tsPlatformUser.getHead();
    }



    @RequestMapping("instance")
    @ResponseBody
    public List<ServiceInstance> showInfo(){
        List<ServiceInstance> instances = discoveryClient.getInstances("provider-user");
        return instances;
    }

    /**
     * 使用Eureka Feign远程调用user服务接口
     * @return
     */
    @RequestMapping("queryAll")
    @ResponseBody
    public List<TsPlatformUser> queryAll(){
        List<TsPlatformUser> tsPlatformUserList = userFeign.queryAll();
        return tsPlatformUserList;
    }


    /**
     * 使用Eureka Feign远程调用user服务接口
     * @return
     */
    @HystrixCommand(fallbackMethod = "queryOneFallBack")
    @RequestMapping("queryOne/{id}")
    @ResponseBody
    public TsPlatformUser queryOne(@PathVariable("id") Long id){
        TsPlatformUser tsPlatformUser = userFeign.queryOne(id);
        return tsPlatformUser;
    }

    /**
     * 使用Hysyrix容错的回退方法
     * @param id
     * @return
     */
    public TsPlatformUser queryOneFallBack(Long id){
        TsPlatformUser tsPlatformUser = new TsPlatformUser();
        tsPlatformUser.setId(-1l);
        tsPlatformUser.setNick("默认用户昵称");
        return tsPlatformUser;
    }



    /**
     * 使用Eureka Feign远程调用user服务接口
     * @return
     */
    @RequestMapping("selectOne")
    @ResponseBody
    public TsPlatformUser selectOne(@RequestParam Long id){
        TsPlatformUser tsPlatformUser = userFeign.selectOne(id);
        return tsPlatformUser;
    }

    /**
     * 测试 使用zuul网关访问user服务 post请求
     * @param mobile
     * @return
     */
    @RequestMapping("selectByMobile")
    @ResponseBody
    public TsPlatformUser selectByMobile(@RequestParam String mobile){
        String url = urlConstant.getSelectByMoblie();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("mobile",mobile);
        String json = apiService.getJson(url, map);
        TsPlatformUser tsPlatformUser = JSON.parseObject(json, TsPlatformUser.class);
        return tsPlatformUser;
    }


}
